package com.ruoyi.carbon.service.impl;

import java.util.List;

import com.ruoyi.carbon.domain.*;
import com.ruoyi.carbon.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.carbon.service.IWmsOutWarehouseApplyService;

/**
 * 出库申请Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-11-22
 */
@Service
public class WmsOutWarehouseApplyServiceImpl implements IWmsOutWarehouseApplyService 
{
    @Autowired
    private WmsOutWarehouseApplyMapper wmsOutWarehouseApplyMapper;

    @Autowired
    private WmsMaterialInfoMapper wmsMaterialInfoMapper;
    @Autowired
    private WmsWarehouseMapper wmsWarehouseMapper;
    @Autowired
    private ScmSaleCustomerMapper scmSaleCustomerMapper;
    @Autowired
    private ScmPurchaseContractMapper scmPurchaseContractMapper;

    /**
     * 查询出库申请
     * 
     * @param id 出库申请主键
     * @return 出库申请
     */
    @Override
    public WmsOutWarehouseApply selectWmsOutWarehouseApplyById(Long id)
    {
        WmsOutWarehouseApply outWarehouseApply = wmsOutWarehouseApplyMapper.selectWmsOutWarehouseApplyById(id);
        List<WmsOutWarehouseApplyDetail> list = outWarehouseApply.getWmsOutWarehouseApplyDetailList();
        if (!(list.isEmpty())){
            list.forEach(detail -> {
                WmsWarehouse wmsWarehouse = wmsWarehouseMapper.selectWmsWarehouseById(detail.getWarehouseId());
                String regionName = wmsWarehouseMapper.findRegionName(detail.getWhRegionId());
                String locationName = wmsWarehouseMapper.findLocationName(detail.getWhLocationId());
                if (wmsWarehouse != null) {
                    detail.setWarehouseName(wmsWarehouse.getName());
                }
                detail.setWhLocationName(locationName);
                detail.setWhRegionName(regionName);

                WmsMaterialInfo materialInfo = wmsMaterialInfoMapper.selectWmsMaterialInfoById(detail.getMaterialId());
                detail.setMaterialInfo(materialInfo);
            });
        }

        return outWarehouseApply;
    }

    /**
     * 查询出库申请列表
     * 
     * @param wmsOutWarehouseApply 出库申请
     * @return 出库申请
     */
    @Override
    public List<WmsOutWarehouseApply> selectWmsOutWarehouseApplyList(WmsOutWarehouseApply wmsOutWarehouseApply)
    {
        List<WmsOutWarehouseApply> list = wmsOutWarehouseApplyMapper.selectWmsOutWarehouseApplyList(wmsOutWarehouseApply);
        list.forEach(i -> {

            ScmSaleCustomer scmSaleCustomer = scmSaleCustomerMapper.selectScmSaleCustomerById(i.getCustomerId());
            if (scmSaleCustomer != null){
                i.setCustomerName(scmSaleCustomer.getName());
            }

            ScmPurchaseContract scmPurchaseContract = scmPurchaseContractMapper.selectScmPurchaseContractById(i.getContractId());
            if (scmPurchaseContract != null){
                i.setContractNo(scmPurchaseContract.getContractNo());
            }
        });
        return list;
    }

    /**
     * 新增出库申请
     * 
     * @param wmsOutWarehouseApply 出库申请
     * @return 结果
     */
    @Transactional
    @Override
    public int insertWmsOutWarehouseApply(WmsOutWarehouseApply wmsOutWarehouseApply)
    {
        int rows = wmsOutWarehouseApplyMapper.insertWmsOutWarehouseApply(wmsOutWarehouseApply);
        insertWmsOutWarehouseApplyDetail(wmsOutWarehouseApply);
        return rows;
    }

    /**
     * 修改出库申请
     * 
     * @param wmsOutWarehouseApply 出库申请
     * @return 结果
     */
    @Transactional
    @Override
    public int updateWmsOutWarehouseApply(WmsOutWarehouseApply wmsOutWarehouseApply)
    {

        return wmsOutWarehouseApplyMapper.updateWmsOutWarehouseApply(wmsOutWarehouseApply);
    }

    /**
     * 批量删除出库申请
     * 
     * @param ids 需要删除的出库申请主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWmsOutWarehouseApplyByIds(Long[] ids)
    {
        wmsOutWarehouseApplyMapper.deleteWmsOutWarehouseApplyDetailByApplyIds(ids);
        return wmsOutWarehouseApplyMapper.deleteWmsOutWarehouseApplyByIds(ids);
    }

    /**
     * 删除出库申请信息
     * 
     * @param id 出库申请主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWmsOutWarehouseApplyById(Long id)
    {
        wmsOutWarehouseApplyMapper.deleteWmsOutWarehouseApplyDetailByApplyId(id);
        return wmsOutWarehouseApplyMapper.deleteWmsOutWarehouseApplyById(id);
    }

    /**
     * 新增出库申请明细信息
     * 
     * @param wmsOutWarehouseApply 出库申请对象
     */
    public void insertWmsOutWarehouseApplyDetail(WmsOutWarehouseApply wmsOutWarehouseApply)
    {
        List<WmsOutWarehouseApplyDetail> wmsOutWarehouseApplyDetailList = wmsOutWarehouseApply.getWmsOutWarehouseApplyDetailList();
        Long id = wmsOutWarehouseApply.getId();
        if (StringUtils.isNotNull(wmsOutWarehouseApplyDetailList))
        {
            List<WmsOutWarehouseApplyDetail> list = new ArrayList<WmsOutWarehouseApplyDetail>();
            for (WmsOutWarehouseApplyDetail wmsOutWarehouseApplyDetail : wmsOutWarehouseApplyDetailList)
            {
                wmsOutWarehouseApplyDetail.setApplyId(id);
                list.add(wmsOutWarehouseApplyDetail);
            }
            if (list.size() > 0)
            {
                wmsOutWarehouseApplyMapper.batchWmsOutWarehouseApplyDetail(list);
            }
        }
    }
}
